<?php
namespace common\helps;
use yii;
class tree
{
    static public $treeList=array();   //存放无限极分类结果

    public function __construct()
    {
        self::$treeList = array();
    }


    /**
     * 递归树型结构
     * @param $data
     * @param  $pid
     * @return array
     * @author hey_heyang(635870453@qq.com)
     */
    public function create($data, $pid=0,$top_id=0)
    {
        foreach($data as $key => $value){
            if($value['pid'] == $pid){
                if($pid==0){
                    self::$treeList[$value['id']]=$value;
                    $top_id=$value['id'];
                }else{
                    if($value['level']==2){ //二级操作
                        self::$treeList[$top_id]['child'][$value['id']]=$value;
                    }elseif($value['level']==3){ //三级操作
                        self::$treeList[$top_id]['child'][$value['pid']]['child'][$value['id']]=$value;
                    }

                }
                unset($data[$key]);
                self::create($data,$value['id'],$top_id);
            }
        }


        return self::$treeList;
    }
}